/**
 * Created by Administrator on 16-10-21.
 */
Ext.onReady(function () {
    Ext.QuickTips.init();
    var ssxmxx_grid_row;
    var ssxmxx_form = new Ext.form.FormPanel({
        labelAlign: 'right',
        frame: true,
        labelWidth: 150,
        url: '/index/ssxmxx/uppt.action',
        bodyStyle: 'padding:30px',
        reader: ssxmxx_reader,
        items: [
            {
                xtype: 'textfield',
                fieldLabel: '实施项目编码',
//                readOnly: true,
//                disabled :true,
                width: 150,
                name: 'obitcd',
                allowBlank:false,
                msgTarget:"side"
            },
            {
                xtype: 'textfield',
                fieldLabel: '实施项目名称',
//                readOnly: true,
//                disabled :true,
                width: 150,
                name: 'obitnm'
            },
            {
                xtype: 'textarea',
                fieldLabel: '备注',
                width: 150,
                name: 'nt'
            }
        ]
    });

    var ssxmxx_win = new Ext.Window({
        layout: 'fit',
        title: '实施项目信息更改',
        width: '500',
        height: '300',
        closeAction: 'hide',
        constrain: true,
        buttonAlign: 'center',
        resizable: false,
        modal: true,
        plain: true,

        items: [ssxmxx_form],
        buttons: [
            {
                text: '修改',
                handler: function () {
                    if(ssxmxx_form.getForm().isValid()){
                        ssxmxx_form.getForm().submit({
                            success: function (form, action) {
                                Ext.Msg.alert('提示',
                                    '提交成功！');
                                ssxmxx_form.getForm().reset();
                                ssxmxx_win.hide();
                                ssxmxx_store.reload({
                                    params: {
                                        start : 0,
                                        limit: 30
                                    }
                                });
                            },
                            failure: function () {
                                Ext.Msg.alert('提示',
                                    '提交失败！');
                            }

                        });
                    }

                }},
            {
                text: '取消',
                handler: function () {
                    ssxmxx_win.hide();
                }
            }
        ]
    });
    ssxmxx_win.on('show', function () {
//        alert(bxdj_grid_row);
        var rec = ssxmxx_store.getAt(ssxmxx_grid_row);
//        alert(rec.get("ucode"));
        ssxmxx_form.getForm().loadRecord(rec);
//        cyxm_combo.setValue(rec.get('obitmcd'));
    });


    //reader用于解析bxdj_store的内容
    var ssxmxx_reader = new Ext.data.JsonReader({
        totalProperty: 'totalProperty',
        root: 'root'
    }, [
        {
            name: 'obitcd'
        },
        {
            name: 'obitnm'
        },
        {
            name: 'nt'
        }
    ]);
    //ssxmxx_store加载内容
    var ssxmxx_store = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/index/ssxmxx/all.action'
        }),
        reader: ssxmxx_reader
    });

    //表格的列头
    var ssxmxx_colModel = new Ext.grid.ColumnModel([
        new Ext.grid.RowNumberer({
            header: '编号',
            width: 35
        }),
        {
            header: "实施项目编码",
            sortable: true,
            dataIndex: 'obitcd',
            width: 370
        }, {
            header: "实施项目名称",
            sortable: true,
            dataIndex: 'obitnm',
            width: 300
        }, {
            header: "备注",
            sortable: true,
            dataIndex: 'nt',
            width: 300
        }, {
            xtype: 'actioncolumn',
            header: "操作",
            items: [
                {
                    icon: '/ext/cog_edit.png',  // Use a URL in the icon config
                    tooltip: '修改',
                    handler: function (grid, rowIndex, colIndex) {
                        var rec = ssxmxx_store.getAt(rowIndex);
                        ssxmxx_grid_row = rowIndex;
                        ssxmxx_win.show();
                    }
                },
                {
                    icon: '/ext/delete.gif',  // Use a URL in the icon config
                    tooltip: '删除',
                    handler: function (grid, rowIndex, colIndex) {
                        var rec = ssxmxx_store.getAt(rowIndex);
                        Ext.MessageBox.confirm('删除', '确认删除？', function (btn) {
                            if (btn == 'yes') {
                                Ext.Ajax.request({
                                    url: '/index/ssxmxx/dele.action',
                                    params: {
                                        obitcd: rec.get('obitcd')
                                    },
                                    success: function (response) {
                                        Ext.Msg.alert('提示',
                                            '删除成功！');
                                        ssxmxx_store.load({
                                            params: {
                                                start : 0,
                                                limit: 30
                                            }
                                        });

                                    },
                                    failure: function (response) {
                                        Ext.Msg.alert('提示',
                                            '删除失败！');
                                    }

                                });

                            }

                        });
                    }
                }

            ]
        }

    ]);

    var ssxmxx_addform = new Ext.form.FormPanel({
        labelAlign: 'right',
        frame: true,
        labelWidth: 150,
        url: '/index/ssxmxx/add.action',
        bodyStyle: 'padding:30px',
        reader: ssxmxx_reader,
        items: [
            {
                xtype: 'textfield',
                fieldLabel: '实施项目编码',
//                readOnly: true,
//                disabled :true,
                width: 150,
                name: 'obitcd',
                allowBlank:false,
                msgTarget:"side"
            },
            {
                xtype: 'textfield',
                fieldLabel: '实施项目名称',
//                readOnly: true,
//                disabled :true,
                width: 150,
                name: 'obitnm'
                //allowBlank:false,
                //msgTarget:"side"
            },
            {
                xtype: 'textarea',
                fieldLabel: '备注',
                width: 150,
                name: 'nt'
            }
        ]
    });

    var ssxmxx_addwin = new Ext.Window({
        layout: 'fit',
        title: '实施项目信息增加',
        width: '500',
        height: '300',
        closeAction: 'hide',
        constrain: true,
        buttonAlign: 'center',
        resizable: false,
        modal: true,
        plain: true,

        items: [ssxmxx_addform],
        buttons: [
            {
                text: '增加',
                handler: function () {
                    if(ssxmxx_addform.getForm().isValid()){
                        ssxmxx_addform.getForm().submit({
                            success: function (form, action) {
                                Ext.Msg.alert('提示',
                                    '提交成功！');
                                ssxmxx_addform.getForm().reset();
                                ssxmxx_addwin.hide();
                                ssxmxx_store.reload({
                                    params: {
                                        start : 0,
                                        limit: 30
                                    }
                                });
                            },
                            failure: function (form, action) {
                                Ext.Msg.alert('提示',
                                    '提交失败！');
                            }

                        });
                    }

                }},
            {
                text: '取消',
                handler: function () {
                    ssxmxx_addwin.hide();
                }
            }
        ]
    });

    var ssxmxx_grid = new Ext.grid.GridPanel({
        title: "实施项目信息管理",
        width: 1100,
        height: 670,
        renderTo: 'ssxmxx',
//        region:'center',
        cm: ssxmxx_colModel,
//        collapsed:true,
        collapsible: true,
        autoExpandColumn: 2,
        stripeRows: true,
        loadMask: true,
        store: ssxmxx_store,
        tbar: new Ext.Toolbar(['->',
            {
                icon: '/ext/add.gif',
                tooltip:'增加',
                handler: function () {
                    ssxmxx_addwin.show();
                }
            }
        ]),
        bbar: new Ext.PagingToolbar({
            firstText: '1',
            pageSize: 30,
            store: ssxmxx_store,
            displayInfo: true,
            displayMsg: '显示第 {0} 到 {1}条记录 ，共 {2}条',
            emptyMsg: "没有记录"
        }),
        viewConfig: {
            forceFit: true
        }

    });
    ssxmxx_store.load({
        params: {
            start :0,
            limit:30
        }
    });

});
